#include "Skybox.h"



CSkybox::CSkybox()
{
	D3DXMatrixIdentity(&m_mtxWorld);
	ZeroMemory(m_pTexture, 6 * sizeof(LPDIRECT3DTEXTURE9));
	m_pVb = NULL;
}

CSkybox::~CSkybox()
{

}

BOOL CSkybox::InitSkyboxTexture(LPDIRECT3DDEVICE9 d3ddv)
{
	HRESULT hr;

	hr  = D3DXCreateTextureFromFile(d3ddv, Sb_Name_Front, &m_pTexture[0]);
	hr &= D3DXCreateTextureFromFile(d3ddv, Sb_Name_Back, &m_pTexture[1]);
	hr &= D3DXCreateTextureFromFile(d3ddv, Sb_Name_Left, &m_pTexture[2]);
	hr &= D3DXCreateTextureFromFile(d3ddv, Sb_Name_Right, &m_pTexture[3]);
	hr &= D3DXCreateTextureFromFile(d3ddv, Sb_Name_Top, &m_pTexture[4]);
	hr &= D3DXCreateTextureFromFile(d3ddv, Sb_Name_Bottom, &m_pTexture[5]);

	if (hr != D3D_OK)
		return FALSE;
	return TRUE;
}

void CSkybox::BuildSkybox(LPDIRECT3DDEVICE9 d3ddv)
{

	CVertex *pVertexBuffer = NULL;
	int count = 0;


	d3ddv->CreateVertexBuffer(24 * sizeof(CVertex), D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC, FVF_TEX, D3DPOOL_DEFAULT, &m_pVb, NULL);
	m_pVb->Lock(0, 24 * sizeof(CVertex), (void**)&pVertexBuffer, D3DLOCK_DISCARD);
	
	// Front face
	pVertexBuffer[count++]	=	CVertex( -10.0f,-10.0f,  10.0f, 0.00f, 1.00f );
	pVertexBuffer[count++]	=	CVertex( -10.0f, 10.0f,  10.0f, 0.00f, 0.00f );
	pVertexBuffer[count++]	=	CVertex(  10.0f,-10.0f,  10.0f, 1.00f, 1.00f );
	pVertexBuffer[count++]	=	CVertex(  10.0f, 10.0f,  10.0f, 1.00f, 0.00f );

	// Back Face
	pVertexBuffer[count++]	=	CVertex(  10.0f,-10.0f, -10.0f, 0.00f, 1.00f );
	pVertexBuffer[count++]	=	CVertex(  10.0f, 10.0f, -10.0f, 0.00f, 0.00f );
	pVertexBuffer[count++]	=	CVertex( -10.0f,-10.0f, -10.0f, 1.00f, 1.00f );
	pVertexBuffer[count++]	=	CVertex( -10.0f, 10.0f, -10.0f, 1.00f, 0.00f );

	// Left Face
	pVertexBuffer[count++]	=	CVertex( -10.0f,-10.0f, -10.0f, 0.00f, 1.00f );
	pVertexBuffer[count++]	=	CVertex( -10.0f, 10.0f, -10.0f, 0.00f, 0.00f );
	pVertexBuffer[count++]	=	CVertex( -10.0f,-10.0f,  10.0f, 1.00f, 1.00f );
	pVertexBuffer[count++]	=	CVertex( -10.0f, 10.0f,  10.0f, 1.00f, 0.00f );

	// Right Face
	pVertexBuffer[count++]	=	CVertex(  10.0f,-10.0f,  10.0f, 0.00f, 1.00f );
	pVertexBuffer[count++]	=	CVertex(  10.0f, 10.0f,  10.0f, 0.00f, 0.00f );
	pVertexBuffer[count++]	=	CVertex(  10.0f,-10.0f, -10.0f, 1.00f, 1.00f );
	pVertexBuffer[count++]	=	CVertex(  10.0f, 10.0f, -10.0f, 1.00f, 0.00f );
	
	// Top Face
	pVertexBuffer[count++]	=	CVertex( -10.0f, 10.0f,  10.0f, 0.00f, 1.00f );
	pVertexBuffer[count++]	=	CVertex( -10.0f, 10.0f, -10.0f, 0.00f, 0.00f );
	pVertexBuffer[count++]	=	CVertex(  10.0f, 10.0f,  10.0f, 1.00f, 1.00f );
	pVertexBuffer[count++]	=	CVertex(  10.0f, 10.0f, -10.0f, 1.00f, 0.00f );

	// Bottom Face
	pVertexBuffer[count++]	=	CVertex( -10.0f,-10.0f, -10.0f, 0.00f, 1.00f );
	pVertexBuffer[count++]	=	CVertex( -10.0f,-10.0f,  10.0f, 0.00f, 0.00f );
	pVertexBuffer[count++]	=	CVertex(  10.0f,-10.0f, -10.0f, 1.00f, 1.00f );
	pVertexBuffer[count++]	=	CVertex(  10.0f,-10.0f,  10.0f, 1.00f, 0.00f );
	
	m_pVb->Unlock();
}

void CSkybox::SetRenderState(LPDIRECT3DDEVICE9 d3ddv)
{
	d3ddv->SetRenderState(D3DRS_ZENABLE, FALSE);
	d3ddv->SetRenderState(D3DRS_LIGHTING, FALSE);
	d3ddv->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
	d3ddv->SetStreamSource(0, m_pVb, 0, sizeof(CVertex));
	d3ddv->SetFVF(FVF_TEX);
}

void CSkybox::Release()
{
	if (m_pTexture)
	{
		delete[] m_pTexture;
	}

	D3DXMatrixIdentity(&m_mtxWorld);
}


void CSkybox::RenderSkybox(LPDIRECT3DDEVICE9 d3ddv)
{

	d3ddv->SetTransform(D3DTS_WORLD, &m_mtxWorld);
	for (int i = 0; i < 6; i++)
	{
		d3ddv->SetTexture(0, m_pTexture[i]);
		d3ddv->DrawPrimitive(D3DPT_TRIANGLESTRIP, i * 4, 2);
	}
}